//#include <pthread.h>
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include "log.h"
#include "FS.h"
#include "definFat32.h"
#include "listas.h"
//#include "consolaFS.h"

static const char *fs_str = "Hello World!\n";
static const char *fs_path = "/hello";

static int fs_getattr(const char *path, struct stat *stbuf){
    int res = 0;

    memset(stbuf, 0, sizeof(struct stat));
    if(strcmp(path, "/") == 0) {
        stbuf->st_mode = S_IFDIR | 0755;
        stbuf->st_nlink = 2;
    }
    else if(strcmp(path, fs_path) == 0) {
        stbuf->st_mode = S_IFREG | 0444;
        stbuf->st_nlink = 1;
        stbuf->st_size = strlen(fs_str);
    }
    else
        res = -ENOENT;

    return res;
}

static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi){
    (void) offset;
    (void) fi;

    if(strcmp(path, "/") != 0)
        return -ENOENT;

    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);
    filler(buf, fs_path + 1, NULL, 0);

    return 0;
}

static int fs_open(const char *path, struct fuse_file_info *fi){
    if(strcmp(path, fs_path) != 0)
        return -ENOENT;

    if((fi->flags & 3) != O_RDONLY)
        return -EACCES;

    return 0;
}

static int fs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
    size_t len;
    (void) fi;
    if(strcmp(path, fs_path) != 0)
        return -ENOENT;

    len = strlen(fs_str);
    if (offset < len) {
        if (offset + size > len)
            size = len - offset;
        memcpy(buf, fs_str + offset, size);
    } else
        size = 0;

    return size;
}

static int fs_truncate(){

	return 0;
}

static int fs_rename(){

	return 0;
}

static int fs_write(){

	return 0;
}

static int fs_mkdir(){

	return 0;
}

static int fs_create(){

	return 0;
}

static int fs_flush(){

	return 0;
}

static int fs_release(){

	return 0;
}

static struct fuse_operations fs_oper = {
    .getattr	= fs_getattr,
    .readdir	= fs_readdir,
    .open	    = fs_open,
    .read 	    = fs_read,
    .truncate   = fs_truncate,
    .rename     = fs_rename,
    .write      = fs_write,
    .mkdir      = fs_mkdir,
    .create     = fs_create,
    .flush      = fs_flush,
    .release    = fs_release
};

int main(int argc, char* argv[])
{
	//leerBootSector();
	//leerFAT();
	iniciar_fat32(const char *path);//VER ARGUMENTOS
	obtener_cant_clusters_libres(t_fat32 *fat32);//VER ARGUMENTOS
	//obtener_cant_clusters_libres_en_un_vector_();
	crearPoolDeConecciones();

	return fuse_main(argc, argv, &fs_oper, NULL);
}

